<template>
    <div class="order-sum" v-loading="loading">
        <div>
            <span class="down-execel">
                <Poptip v-model="showFilter" placement="bottom-end">
                    <i class="el-icon-search poptip-condition-add"></i>
                    <a href="javascript:;" class='poptip-condition-add'>列表查找</a>
                    <div slot="content">
                        <Poptip v-model="showFilterCondition" placement="bottom-start" class='poptip-condition'>
                            <i class="el-icon-plus"></i>
                            <a href="javascript:;">添加过滤条件</a>
                            <div slot="content" class="condition-list">
                                <ul>
                                    <li
                                        v-for="(item, index) in selectHead"
                                        :key="index"
                                        class='poptip-condition-item'
                                        @click="addFilterCondition(item)">
                                        {{item.label}}
                                    </li>
                                </ul>
                            </div>
                        </Poptip>
                        <el-form :model="conditionForm" @submit.native.prevent ref="conditionForm" label-width="120px" class="demo-dynamic">
                            <el-form-item
                                v-for="(condition, index) in conditionForm.condition"
                                :label="condition.label"
                                :key="condition.key"
                                :prop="'condition.' + index + '.value'"
                                :rules="{required: true, message: condition.label + '不能为空', trigger: 'blur'}">
                                <el-select
                                    v-if='condition.label === "流程人员"'
                                    v-model="condition.value"
                                    placeholder="请选择流程人员"
                                    size="small"
                                    filterable
                                >
                                    <el-option
                                        v-for="item in processOptions"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id"
                                    ></el-option>
                                </el-select>
                                <el-select
                                    v-else-if='condition.label === "代理人"'
                                    v-model="condition.value"
                                    placeholder="请选择代理人"
                                    size="small"
                                    filterable
                                >
                                    <el-option
                                        v-for="item in agentOptions"
                                        :key="item.id"
                                        :label="item | agentNumAddName"
                                        :value="item.name"
                                    ></el-option>
                                </el-select>
                                <el-date-picker
                                    v-model="condition.value"
                                    type="month"
                                    placeholder="选择日期"
                                    value-format="yyyy-MM"
                                    size="small"
                                    :validate-event='false'
                                    v-else-if='condition.label === "时间选择"'>
                                </el-date-picker>
                                <el-input v-else size="small" v-model="condition.value" @keyup.enter.native="submitsearch('conditionForm')"></el-input>
                                <el-button @click.stop="removeDomain(condition)" size="mini">删除</el-button>
                            </el-form-item>
                        </el-form>

                        <div class="btns">
                            <el-button @click="submitsearch('conditionForm')" size="mini">查询</el-button>
                            <el-button @click="resetForm('conditionForm')" size="mini">重置</el-button>
                        </div>
                    </div>
                </Poptip>
            </span>
        </div>
        <el-table
            :data="tableData"
            ref="order-table"
            border
        >
            <el-table-column
                width="250"
                align="center"
                prop="type"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                v-for="(item, index) in tableHeader"
                :key="index"
                :label="item + ' (件)'"
                align="center"
                :prop="item"
            ></el-table-column>
            <el-table-column
                label="合计 (件)"
                align="center"
                prop="sum"
            ></el-table-column>
        </el-table>
    </div>
</template>

<script>
export default {
    components: {
    },

    data() {
        return {
            loading: false,
            tableData: [],
            tableHeader: [],
            submitCount: {},
            finishaCount: {},
            search_params: {},
            showFilter: false,
            showFilterCondition: false,
            // 筛选条件
            conditionForm: {
                condition: []
            },
            selectHead: [
                {
                    label: '时间选择',
                    props: 'month'
                }
            ],
        }
    },

    created() {
        this.getTableData()
    },

    methods: {
        getTableData() {
            this.loading = true
            this.$axios.get('/api/order/order_count', {
                params: this.search_params
            }).then(res => {
                if (res.data.code == 200) {
                    this.tableData = []
                    this.tableHeader = res.data.data.month
                    let sum1 = 0,
                        sum2 = 0;
                    for (let i in res.data.data.submitCount) {
                        sum1 += res.data.data.submitCount[i]
                    }
                    for (let i in res.data.data.finishaCount) {
                        sum2 += res.data.data.finishaCount[i]
                    }
                    res.data.data.submitCount.type = '近五个月累计提交订单总数'
                    res.data.data.submitCount.sum = sum1
                    this.tableData.push(res.data.data.submitCount)
                    res.data.data.finishaCount.type = '近五个月累计完成订单总数'
                    res.data.data.finishaCount.sum = sum2
                    this.tableData.push(res.data.data.finishaCount)
                    this.loading = false
                } else {
                    this.loading = false
                    this.$message({
                        type: 'error',
                        message: res.data.message
                    })
                }
            })
        },
        addFilterCondition(item) {
            let filterItem = {
                value: '',
                label: item.label,
                parameter: item.props,
                key: Date.now()
            }

            if (this.conditionForm.condition.length > 0) {
                let conditionlabels = []
                for (let i = 0; i < this.conditionForm.condition.length; i++) {
                    conditionlabels.push(this.conditionForm.condition[i].label)
                }
                let Check = conditionlabels.indexOf(item.label)
                Check == -1 ? this.conditionForm.condition.push(filterItem) : this.$message.error('请勿重复添加')
            } else {
                this.conditionForm.condition.push(filterItem)
            }

            this.showFilterCondition = false;
        },
        removeDomain(item) {
            let index = this.conditionForm.condition.indexOf(item)
            if (index !== -1) {
                this.conditionForm.condition.splice(index, 1)
            }
            let props = JSON.parse(JSON.stringify(this.conditionForm.condition))
            let params = {}
            props.forEach(v => {
                params[v.parameter] = v.value
            })
            this.search_params = params
            this.showFilter = false
            this.getTableData()
        },
        submitsearch(formName) {
            let that = this
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let props = JSON.parse(JSON.stringify(this.conditionForm.condition))
                    let params = {}
                    props.forEach(v => {
                        params[v.parameter] = v.value
                    })
                    this.$refs['order-table'].clearSort()
                    this.search_params = {...params}
                    this.getTableData()
                    this.showFilterCondition = false
                    this.showFilter = false
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
            this.conditionForm.condition.forEach(v => {
              if (v.value) {
                v.value = null
              }
            });
            this.$refs['order-table'].clearSort()
            this.search_params = {}
            this.$nextTick(() => {
              this.$refs[formName].clearValidate()
            })
            // this.getTableData()
            // this.showFilterCondition = false
            // this.showFilter = false
        },
    },

    watch: {

    }
}
</script>

<style lang="less" scoped>
.order-sum {
    height: 100%;
    /deep/.el-table {
		.img-wrap {
			display: flex;
            justify-content: center;
            > div {
                height: 23px;
            }
			.el-image {
                width: 23px;
				height: 23px;
			}
		}
	}
    /deep/.ivu-poptip-inner {
		min-width: 450px;
		// height: 300px;
		overflow-y: scroll;
	}

	/deep/.poptip-condition .ivu-poptip-inner {
		min-width: 180px;
	}

	.demo-dynamic {
		padding-left: 10px;

		.el-form-item {
			margin-bottom: 12px;
		}

		.el-input {
			width: 240px;
		}

		.el-date-editor {
			width: 240px;
		}

		.el-button {
			padding: 9px 15px;
			vertical-align: baseline;
		}

		.el-form-item__error {
			padding-top: 0;
		}
	}

	.poptip-condition {
		display: block;
		margin-top: 10px;

		.poptip-condition-item {
			line-height: 25px;
			cursor: pointer;
		}

		.poptip-condition-add {
			font-size: 14px;
		}
	}

	.btns {
		margin-top: 10px;
    }
    .el-select {
        width: 240px;
    }
}
</style>
